package io.renren.modules.base.service.impl;

import io.renren.common.utils.RandomId;
import io.renren.modules.base.dao.BaseTaskWorkitemDao;
import io.renren.modules.base.entity.BaseTaskWorkitemEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import io.renren.modules.base.dao.BaseWorkitemInfoDao;
import io.renren.modules.base.entity.BaseWorkitemInfoEntity;
import io.renren.modules.base.service.BaseWorkitemInfoService;



@Service("baseWorkitemInfoService")
public class BaseWorkitemInfoServiceImpl implements BaseWorkitemInfoService {
	@Autowired
	private BaseWorkitemInfoDao baseWorkitemInfoDao;
	@Autowired
	private BaseTaskWorkitemDao baseTaskWorkitemDao;
	
	@Override
	public BaseWorkitemInfoEntity queryObject(String id){
		return baseWorkitemInfoDao.queryObject(id);
	}
	
	@Override
	public List<BaseWorkitemInfoEntity> queryList(Map<String, Object> map){
		return baseWorkitemInfoDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return baseWorkitemInfoDao.queryTotal(map);
	}
	
	@Override
	public void save(BaseWorkitemInfoEntity baseWorkitemInfo){
		List<BaseWorkitemInfoEntity> baseWorkitemInfoEntityList = baseWorkitemInfoDao.queryTask(baseWorkitemInfo.getTaskId());
		if (baseWorkitemInfoEntityList != null && baseWorkitemInfoEntityList.size()>0){
			baseWorkitemInfo.setOrder(baseWorkitemInfoEntityList.size()+1);
		}else {
			baseWorkitemInfo.setOrder(1);
		}
		String workitemId = RandomId.createId()+"";
        baseWorkitemInfo.setId(workitemId);
        if ("".equals(baseWorkitemInfo.getDeliveryGoods()) || baseWorkitemInfo.getDeliveryGoods() == null){
			baseWorkitemInfo.setDeliveryGoods("无");
		}
		baseWorkitemInfoDao.save(baseWorkitemInfo);
		BaseTaskWorkitemEntity baseTaskWorkitemEntity = new BaseTaskWorkitemEntity();
		baseTaskWorkitemEntity.setId(RandomId.createId()+"");
		baseTaskWorkitemEntity.setTaskId(baseWorkitemInfo.getTaskId());
		baseTaskWorkitemEntity.setWoekitmeId(workitemId);
		baseTaskWorkitemDao.save(baseTaskWorkitemEntity);
	}
	
	@Override
	public void update(BaseWorkitemInfoEntity baseWorkitemInfo){
		baseWorkitemInfoDao.update(baseWorkitemInfo);
	}

	@Override
	public void updateWorkitemInfo(BaseWorkitemInfoEntity baseWorkitemInfo){
		BaseTaskWorkitemEntity baseTaskWorkitemEntity = new BaseTaskWorkitemEntity();
		baseTaskWorkitemEntity.setWoekitmeId(baseWorkitemInfo.getId());
		baseTaskWorkitemEntity.setTaskId(baseWorkitemInfo.getTaskId());
		baseTaskWorkitemDao.updateWorkitem(baseTaskWorkitemEntity);
		baseWorkitemInfoDao.update(baseWorkitemInfo);
	}
	
	@Override
	public void delete(String id){
		baseWorkitemInfoDao.delete(id);
	}
	
	@Override
	public void deleteBatch(String[] ids){
		baseWorkitemInfoDao.deleteBatch(ids);
	}

	@Override
	public void deleteWorkitem(String workitemId) {
		baseWorkitemInfoDao.delete(workitemId);
		baseTaskWorkitemDao.deleteWorkitem(workitemId);
	}


}
